﻿using BLL;
using Model;
using PSIMIS2019.Comm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PSIMIS2019.SubPages
{
    public partial class _PurchaseBill : PSIMIS2019.Templates.BasePurchaseBillForm
    {
        private PurchaseBillBLL bl;
        private DateTime currentTime = DateTime.Now;//当前时间
        public static PurchaseDetail addDetail;

        private Operation opera;//操作类型

        public _PurchaseBill()
        {
            InitializeComponent();
            DataBinding.SupplierComboBox(cbSupplier);//供应商下拉菜单
            bl = new PurchaseBillBLL();
            detailList = new List<PurchaseDetail>();
            opera = Operation.NONE;
            InitialCtrol();
        }
        /// <summary>
        /// 生成订单编号
        /// </summary>
        private string CreatePurchaseBillCode()
        {
            //Ticks——获取去当前实例日期和时间的间隔
            //ToFileTimeUtc()——获取Windows文件时间，用DateTime来替换
            //GetHashCode()——该实例的哈希代码
            var value = DateTime.Now.GetHashCode().ToString() + System.Environment.TickCount;
            value = value.Substring(value.Length - 12);//取value右边12个字符
            return value;
        }
        private void InitialCtrol()
        {
            btnAddDetail.Enabled = btnDeleteDetail.Enabled = false;
            btnConfirm.Enabled = btnCancel.Enabled = false;
            btnAdd.Enabled = btnModify.Enabled = btnDelete.Enabled = true;
            btnSearching.Enabled = true;
            if (opera == Operation.INSERT)
            {
                btnModify.Enabled = btnDelete.Enabled = btnSearching.Enabled = false;
                dtpPurchaseDate.Enabled = cbSupplier.Enabled = txtMemo.Enabled = true;
                btnAddDetail.Enabled = btnDeleteDetail.Enabled = true;
                btnConfirm.Enabled = btnCancel.Enabled = true;
                txtOnPress.Text = "（待定）";
                txtPurchaseID.Text = CreatePurchaseBillCode();
            }
            if (opera == Operation.UPDATE)
            {
                btnAdd.Enabled = btnDelete.Enabled = btnSearching.Enabled = false;
                dtpPurchaseDate.Enabled = cbSupplier.Enabled = txtMemo.Enabled = true;
                btnAddDetail.Enabled = btnDeleteDetail.Enabled = true;
                btnConfirm.Enabled = btnCancel.Enabled = true;
                txtOnPress.Text = "执行（待审核）";
            }
            if (opera == Operation.DELETE)
            {
                btnAdd.Enabled = btnModify.Enabled = btnSearching.Enabled = false;
                dtpPurchaseDate.Enabled = cbSupplier.Enabled = txtMemo.Enabled = true;
                btnAddDetail.Enabled = btnDeleteDetail.Enabled = true;
                btnConfirm.Enabled = btnCancel.Enabled = true;
                txtOnPress.Text = "执行（待审核）";
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            opera = Operation.INSERT;
            InitialCtrol();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (bo == null)
            {
                MessageBox.Show("请选择要修改的订单！", "系统提示：");
                return;
            }
            if (bo.OnProcess != 0)
            {
                MessageBox.Show("该订单已完成或被撤销，不能被修改！", "系统提示：");
                return;
            }
            if (bo.Examiner == null || bo.Examiner == "")
            {
                opera = Operation.UPDATE;
                InitialCtrol();
            }
            else
            {
                MessageBox.Show("该订单已审核，不允许修改", "系统提示：");
                return;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bo == null)
            {
                MessageBox.Show("请选择要删除的订单！", "系统提示：");
                return;
            }
            if (bo.OnProcess != 0)
            {
                MessageBox.Show("该订单已完成或被撤销，不能被删除！", "系统提示：");
                return;
            }
            if (bo.Examiner == null || bo.Examiner == "")
            {
                opera = Operation.DELETE;
                InitialCtrol();
            }
            else
            {
                MessageBox.Show("该订单已审核，不允许删除", "系统提示：");
                return;
            }
        }
        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            Form productList = new _ChooseProducts();
            productList.ShowDialog();
            if (addDetail != null)
            {
                foreach (PurchaseDetail item in detailList)
                {
                    if (item.ProductID == addDetail.ProductID)
                    {
                        MessageBox.Show("产品已存在，请勿重复插入同一产品！", "系统提示：");
                        return;
                    }
                }
                addDetail.PurchaseID = txtPurchaseID.Text.Trim();
                addDetail.PurchaseDetailID = detailList.Count + 1;
                detailList.Add(addDetail);
                BindData();
            }
        }
        private void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (pDetail == null)
            {
                MessageBox.Show("请选择详单条目进行删除操作！", "系统提示：");
                return;
            }
            DialogResult dialogString = MessageBox.Show("您确定要删除该详单数据吗？", "系统提示：", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogString == DialogResult.Yes)
            {
                detailList.Remove(pDetail);
                int i = 1;
                foreach (PurchaseDetail item in detailList)
                {
                    item.PurchaseDetailID = i++;//对详单ID进行累加赋值
                }
                BindData();
                pDetail = null;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (opera == Operation.INSERT)
            {
                bo = new PurchaseBill();
                bo.PurchaseDate = dtpPurchaseDate.Value;
                bo.PurchaseBillID = txtPurchaseID.Text.Trim();
                bo.SupplierID = cbSupplier.SelectedValue.ToString();
                bo.Memo = txtMemo.Text;
                bo.OnProcess = 0;//执行状态
                bo.Clerk = "800003";
                //生成详单集合
                //detailList = new List<PurchaseDetail>();
                bo.PurchaseDetailList = detailList;
                if (bl.Edit(bo, opera.ToString()))
                {
                    MessageBox.Show("新订单已成功生成！", "系统提示：");
                    lbPurchaseBill.DataSource = null;
                }
                else
                    MessageBox.Show("订单生成失败！", "系统提示：");
            }
            else if (opera == Operation.UPDATE)
            {
                bo = new PurchaseBill();
                bo.PurchaseDate = dtpPurchaseDate.Value;
                bo.PurchaseBillID = txtPurchaseID.Text.Trim();
                bo.SupplierID = cbSupplier.SelectedValue.ToString();
                bo.Memo = txtMemo.Text;
                bo.Clerk = "800003";
                //生成详单集合
                //detailList = new List<PurchaseDetail>();
                bo.PurchaseDetailList = detailList;
                if (bl.Edit(bo, opera.ToString()))
                    MessageBox.Show("新订单已成功更新！", "系统提示：");
                else
                    MessageBox.Show("订单更新失败！", "系统提示：");
            }
            if (opera == Operation.DELETE)
            {
                bo = new PurchaseBill();
                bo.PurchaseDate = dtpPurchaseDate.Value;
                bo.PurchaseBillID = txtPurchaseID.Text.Trim();
                bo.SupplierID = cbSupplier.SelectedValue.ToString();
                bo.Memo = txtMemo.Text;
                bo.Clerk = "800003";
                //生成详单集合
                //detailList = new List<PurchaseDetail>();
                bo.PurchaseDetailList = detailList;
                if (bl.Edit(bo, opera.ToString()))
                {
                    MessageBox.Show("新订单已成功删除！", "系统提示：");
                    lbPurchaseBill.DataSource = null;
                }
                else
                    MessageBox.Show("订单删除失败！", "系统提示：");
            }
            opera = Operation.NONE;
            InitialCtrol();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            opera = Operation.NONE;
            InitialCtrol();
        }
    }
}
